﻿using Newtonsoft.Json;
using System.Xml;
using System.Xml.Linq;

namespace WeWork.SDK.Framework.Helpers
{
    /// <summary>
    /// JSON类
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// 将object转换成json字符串
        /// </summary>
        /// <param name="value">object</param>
        /// <returns></returns>
        public static string ObjectToJson(object value)
        {
            return JsonConvert.SerializeObject(value);
        }

        /// <summary>
        /// 将XmlNode转换成json字符串
        /// </summary>
        /// <param name="value">object</param>
        /// <returns></returns>
        public static string XmlNodeToJson(XmlNode value)
        {
            return JsonConvert.SerializeXmlNode(value);
        }

        /// <summary>
        /// 将XObject转换成json字符串
        /// </summary>
        /// <param name="value">object</param>
        /// <returns></returns>
        public static string XObjectToJson(XObject value)
        {
            return JsonConvert.SerializeXNode(value);
        }

        /// <summary>
        /// 将json字符串转换成对象
        /// </summary>
        /// <param name="value">json字符</param>
        /// <returns></returns>
        public static T JsonToT<T>(string value) where T : class
        {
            return JsonConvert.DeserializeObject<T>(value);
        }

        /// <summary>
        /// 将json字符串转换成Xml
        /// </summary>
        /// <param name="value">json字符</param>
        /// <returns></returns>
        public static XmlDocument JsonToXml(string value)
        {
            return JsonConvert.DeserializeXmlNode(value);
        }

        /// <summary>
        /// 将json字符串转换成XNode
        /// </summary>
        /// <param name="value">json字符</param>
        /// <returns></returns>
        public static XDocument JsonToXNode(string value)
        {
            return JsonConvert.DeserializeXNode(value);
        }
    }
}
